package com.circulardependence.controller;
import com.circulardependence.entity.Personal;
import com.circulardependence.service.PersonalService;
import com.circulardependence.utils.Constant;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * Created by IntelliJ IDEA2021.3
 * @Author: Tenghw
 * @Date: 2022/08/24  21:58
 * @Description:
 */
@Api(tags = "personal个人信息对外接口", basePath = "https://localhost:8080/",description = "the PersonController API") //本controller的功能描述
@Controller
@RequestMapping(path = "/person")
@Slf4j
@SuppressWarnings("all")
public class PersonController {
    @Autowired
    PersonalService personalService;

    //测试@CrossOrigin是否能解决跨域请求
    @ApiOperation(value = "111", notes = "222")
    @CrossOrigin
    @GetMapping(path = "/getById/{id}")
    @ResponseBody
    public Personal getById(@PathVariable("id") int id) {
        log.info("getMyaddressById={}", id);
        return personalService.selectOneById(id);
    }

    //测试响应头Header是否能解决跨域请求
    @GetMapping(path = "/getById1/{id}")
    @ResponseBody
    public Personal getById1(@PathVariable("id") int id,
                             HttpServletRequest request,
                             HttpServletResponse response) {
        log.info("后端处理模拟跨域请求");
        log.info("设置respose的响应头=========start");
        //设置返回的数据类型
        response.setContentType("application/json;charset=UTF-8");
        //设置：Access-Control-Allow-Origin头，处理Session问题
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("P3P", "CP=CAO PSA OUR");
        if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
            response.addHeader("Access-Control-Allow-Method", "POST,GET,TRACE,OPTIONS");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type,Origin,Accept");
            response.addHeader("Access-control-Max-Age", "120");
        }
        log.info("设置respose的响应头=========end");
        return personalService.selectOneById(id);
    }

    //mybatis-plus实现分页查询
    @ApiOperation(value = "mybatis-plus实现分页", notes = "请求参数为json格式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，默认从第1开始", paramType = "query", required = true, dataType = "int",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = Constant.ORDER, value = "asc正序", paramType = "query", dataType = "String",dataTypeClass = String.class)
    })
    //这里是显示你可能返回的http状态，以及原因。比如404 not found, 303 see other
    @ApiResponses(value = {
            @ApiResponse(code = 405, message = "Invalid input", response = Void.class) })
    @PostMapping(path = "/getByCampPages")
    @ResponseBody
    public List<Personal> getByCampPages(@ApiParam(name = "接收json参数", defaultValue = "{}")
                                         @RequestBody Personal personal) {
        log.info("=========PersonController-getByCampPages开始===========");
        return personalService.findByCampPages(personal);
    }

}
